package com.tedu.parkinglot.parkRecord.controller;

import com.alibaba.excel.EasyExcel;
import com.tedu.parkinglot.base.response.JsonResult;
import com.tedu.parkinglot.parkRecord.pojo.dto.ParkRecordQuery;
import com.tedu.parkinglot.parkRecord.pojo.entity.ParkRecord;
import com.tedu.parkinglot.parkRecord.pojo.vo.ParkRecordVO;
import com.tedu.parkinglot.parkRecord.service.impl.ParkRecordServiceImpl;
import com.tedu.parkinglot.parkRecord.utils.TestFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/parkrecord/")
public class ParkRecordController {
    @Autowired
    ParkRecordServiceImpl parkRecordService;

    private List<ParkRecordVO> list = new ArrayList<>();


    @GetMapping("select")
    public JsonResult selectParkRecord(ParkRecordQuery parkRecordQuery){
        log.debug("parkRecordQuery:",parkRecordQuery);
        list=parkRecordService.selectParkRecord(parkRecordQuery);
        return JsonResult.ok(list);

    }

    @GetMapping("download")
    public void download(HttpServletResponse response, ParkRecordQuery parkRecordQuery) throws IOException {
        list=parkRecordService.selectParkRecord(parkRecordQuery);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("停车缴费记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ParkRecordVO.class).sheet("模板").doWrite(list);
    }

}
